#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import xlrd
import os

def check_yusentong_data():
    # 检查最新的Excel文件
    uploads_dir = 'uploads'
    files = [f for f in os.listdir(uploads_dir) if f.endswith(('.xls', '.xlsx'))]
    if not files:
        print("没有找到Excel文件")
        return
    
    # 使用最新的文件
    latest_file = max(files, key=lambda f: os.path.getctime(os.path.join(uploads_dir, f)))
    file_path = os.path.join(uploads_dir, latest_file)
    
    print(f"检查文件: {latest_file}")
    
    try:
        workbook = xlrd.open_workbook(file_path)
        worksheet = workbook.sheet_by_index(0)
        
        # 获取表头
        headers = [str(cell.value) if cell.value else '' for cell in worksheet.row(0)]
        print(f"表头: {headers}")
        
        # 查找于森瞳的所有记录
        yusentong_records = []
        
        for row_idx in range(1, worksheet.nrows):
            row_data = []
            for col_idx in range(worksheet.ncols):
                cell = worksheet.cell(row_idx, col_idx)
                row_data.append(cell.value)
            
            # 检查是否包含于森瞳
            row_str = str(row_data).lower()
            if '于森瞳' in row_str or 'yusentong' in row_str:
                yusentong_records.append(row_data)
        
        print(f"\n找到于森瞳的记录数: {len(yusentong_records)}")
        
        for i, record in enumerate(yusentong_records, 1):
            print(f"\n记录 {i}:")
            for j, (header, value) in enumerate(zip(headers, record)):
                print(f"  {header}: {value}")
        
        # 查找包含"全国链家门店费用"的记录
        print(f"\n=== 查找包含'全国链家门店费用'的所有记录 ===")
        mendiandian_records = []
        
        for row_idx in range(1, worksheet.nrows):
            row_data = []
            for col_idx in range(worksheet.ncols):
                cell = worksheet.cell(row_idx, col_idx)
                row_data.append(cell.value)
            
            # 检查是否包含全国链家门店费用
            row_str = str(row_data)
            if '全国链家门店费用' in row_str:
                mendiandian_records.append(row_data)
        
        print(f"找到'全国链家门店费用'相关记录数: {len(mendiandian_records)}")
        
        for i, record in enumerate(mendiandian_records, 1):
            print(f"\n记录 {i}:")
            for j, (header, value) in enumerate(zip(headers, record)):
                print(f"  {header}: {value}")
                
    except Exception as e:
        print(f"读取Excel文件失败: {e}")

if __name__ == "__main__":
    check_yusentong_data()